package com.lx.util;

import java.sql.*;

public class Demo1 {
    public static void main(String[] args) {
        findAll();
    }
    // 查询所有
    public static void findAll() {
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        PreparedStatement psmt=null;
        String sql,sql2=null;
        try {
            conn=new JDBCUtils().getConnections();
//            stmt=conn.createStatement();
//            sql="select * from student1";
//            rs = stmt.executeQuery(sql);
            //防止sql注入使用prepareStatement
            sql2="select * from student1 where id = ?";
            psmt = conn.prepareStatement(sql2);
            psmt.setInt(1,1);
            rs=psmt.executeQuery();
            while (rs.next()){
                int i = rs.getInt(1);
                String name=rs.getString("name");
                System.out.println(i+"==="+name);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("error");
        }finally {
            JDBCUtils.close(rs, psmt, conn);
            System.out.println("success");
        }
    }
    }
